require 'event_editor/gadgets/switch_button'
require 'gadgets/radio_group'
module EventEditor
  module Commands
    class Switch
      def self.show(base, model, edit_command = [], options = {}, &func)
        @dialog = Gtk::Dialog.new("Switch",
               base,
               Gtk::Dialog::MODAL,
               ["OK", Gtk::Dialog::RESPONSE_OK],
               ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )

        @model = model

        
        @dialog.vbox.add(self.create_panel(base))
        @dialog.vbox.show_all
        
        self.load(edit_command)
        
        response = @dialog.run
        if response == Gtk::Dialog::RESPONSE_OK
          command = self.command
          func.call(command) 
        end
        @dialog.destroy
      end
      
      def self.create_panel(base)
        v_box = Gtk::VBox.new

        @switch_group = ::Gadgets::RadioGroup.new(:vertical)
        @switch_group.add_element('single', 'Single', self.create_single(base), :frame)
        @switch_group.add_element('area', 'Area', self.create_area(base), :frame)

        v_box.add(@switch_group)
        v_box.add(self.create_set_switch)

        return v_box
      end

      def self.create_single(base)
        box = Gtk::HBox.new
        @switch_input = EventEditor::Gadgets::SwitchButton.new(base, @model.switches) do |arr|
          @model.switches = arr
        end
        box.pack_start(@switch_input, false, false)
        return box
      end
      
      def self.create_area(base)
        box = Gtk::HBox.new
        
        @start_switch_no = Gtk::SpinButton.new(0, 9999, 1)
        @end_switch_no = Gtk::SpinButton.new(0, 9999, 1)
        box.add(@start_switch_no)
        box.add(Gtk::Label.new(' - '))
        box.add(@end_switch_no)

        return box
      end
      
      def self.create_set_switch
        frame = Gtk::Frame.new('Set to')
        @combobox = ::Gadgets::Combobox.new([['ON', 'on'], ['OFF', 'off'], ['Reverse', 'reverse']], [String, String])
        frame.add(@combobox)
        return frame
      end

      def self.load(edit_command)
        unless edit_command.empty?
          case edit_command[1]
          when 'single'
            @switch_group.selected_key = edit_command[1]
            @switch_input.value = edit_command[2]
            @combobox.select(edit_command[3])
          when 'area'
            @switch_group.selected_key = edit_command[1]
            @start_switch_no.value = edit_command[2]
            @end_switch_no.value = edit_command[3]
            @combobox.select(edit_command[4])
          else
            @switch_input.value = edit_command[1]
            @combobox.select(edit_command[2])
          end
        end
      end
      
      def self.command
        case @switch_group.selected_key
        when 'single'
          return ['switch', @switch_group.selected_key, @switch_input.value, @combobox.selected_data]
        when 'area'
          return ['switch', @switch_group.selected_key, @start_switch_no.value.floor, @end_switch_no.value.floor, @combobox.selected_data]
        end
      end
      
      def self.command_to_s(model, args, margin = "")
        arr = {'on' => 'ON', 'off' => 'OFF', 'reverse' => 'Reverse'}
        s = ""
        s += '<b>Switch:</b> '
        #s += "[" + args[1].to_s + ": " + model.switches[args[1]] + "] = " + arr[args[2]]
        
        case args[1]
        when 'single'
          s += "[#{args[2]}: #{model.switches[args[2]] }] = #{arr[args[3]]}"
        when 'area'
          s += "[No:#{args[2]}  -  No:#{args[3]}] = #{arr[args[4]]}"
        else
          s += "[#{args[1]}: #{model.switches[args[1]] }] = #{arr[args[2]]}"
        end
        return s
      end
    end
  end
end